package com.sanrenzu.chunbo.http;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sanrenzu.chunbo.bean.zxb.LaoTangBean;
import com.sanrenzu.chunbo.bean.zxb.LunBoBean;
import com.sanrenzu.chunbo.bean.zxb.QiangGouBean;
import com.sanrenzu.chunbo.bean.zxb.ReMaiBean;
import com.sanrenzu.chunbo.bean.zxb.ShouYeDianJiBean;
import com.sanrenzu.chunbo.bean.zxb.ShuiGuoBean;
import com.sanrenzu.chunbo.bean.zxb.TuiJianBean;
import com.sanrenzu.chunbo.bean.zxb.XinXianBean;
import com.sanrenzu.chunbo.config.UrlConfig;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by My on 2016/8/30.
 */
public class HttpUtils {

    //保证http访问工具仅有一个
    private   static  volatile HttpUtils sinleton;
    private Retrofit retrofit;
    private Retrofit retrofitLunbo;

    private IDataBean iDataBean;
    private IDataBean iDataBeanLunbo;

    private HttpUtils() {
        retrofit = createRetrofit();
        retrofitLunbo = createRetrofitLunbo();
        iDataBean = createIDataBean();
        iDataBeanLunbo = createIDataBeanLubo();
    }
    private IDataBean creatChunbo() {
        IDataBean iDataBean =   retrofit.create(IDataBean.class);
        return iDataBean;
    }

    //同步锁定http访问类，保证每次仅一个，锁定后通过类的实例继续调用
    public static HttpUtils getInstance(){
        if(sinleton==null){
            synchronized (HttpUtils.class){
                if (sinleton==null){
                    sinleton = new HttpUtils();
                }
            }
        }
        return sinleton;
    }

    /**
     * Retrofit 实例化
     * @return
     */
    private Retrofit createRetrofit(){
        return new Retrofit.Builder()
                .baseUrl(UrlConfig.Path.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(createGson()))
                .build();
    }

    private Retrofit createRetrofitLunbo(){
        return new Retrofit.Builder()
                .baseUrl(UrlConfig.Path.BASE_FENLEI_URL)
                .addConverterFactory(GsonConverterFactory.create(createGson()))
                .build();
    }
    /**
     * Gson实例化
     * @return
     */
    private Gson createGson(){
        Gson gson = new GsonBuilder()
                //序列化不为空
                .serializeNulls()
                .create();
        return gson;
    }

    /**
     * 接口的实例化
     * @return
     */
    private IDataBean createIDataBean(){
        IDataBean iDataBean =   retrofit.create(IDataBean.class);
        return iDataBean;
    }
    /**
     * 接口的实例化  轮播
     * @return
     */
    private IDataBean createIDataBeanLubo(){
        IDataBean iDataBean =   retrofitLunbo.create(IDataBean.class);
        return iDataBean;
    }

    /**
     * 网络访问的方式，此处为异步  轮播
     * @param callback
     */
    public void getVerticalLunbo(Callback<LunBoBean> callback){
        Call<LunBoBean> call = iDataBeanLunbo.getVerticalData();
        call.enqueue(callback);
    }
    /**
     * 网络访问的方式，此处为异步    抢购
     * @param callback
     */
    public void getVerticalQiangGouBean(Callback<QiangGouBean> callback){
        Call<QiangGouBean> call = iDataBeanLunbo.getVerticalQiangGou();
        call.enqueue(callback);
    }

    /**
     * 网络访问的方式，此处为异步    推荐
     * @param callback
     */
    public void getVerticalTuiJianBean(Callback<TuiJianBean> callback){
        Call<TuiJianBean> call = iDataBeanLunbo.getVerticalTuiJian();
        call.enqueue(callback);
    }
    /**
     * 网络访问的方式，此处为异步    热卖
     * @param callback
     */
    public void getVerticalReMaiBean(Callback<ReMaiBean> callback){
        Call<ReMaiBean> call = iDataBeanLunbo.getVerticalReMai();
        call.enqueue(callback);
    }

    /**
     * 网络访问的方式，此处为异步    老汤
     * @param callback
     */
    public void getVerticalLaoTangBean(Callback<LaoTangBean> callback){
        Call<LaoTangBean> call = iDataBeanLunbo.getVerticalLaoTang();
        call.enqueue(callback);
    }

    /**
     * 网络访问的方式，此处为异步    首页点击
     * @param callback
     */
    public void getVerticalShouYeDianJiBean(Map<String,String> params,Callback<ShouYeDianJiBean> callback){
        Call<ShouYeDianJiBean> call =iDataBeanLunbo.getVerticalShouYe(params);
        call.enqueue(callback);
    }
    /**
     * 网络访问的方式，此处为异步    首页点击
     * @param callback
     */
    public void getVerticalXiangQingJiBean(Callback<ShouYeDianJiBean> callback){
        Call<ShouYeDianJiBean> call =iDataBeanLunbo.getVerticalShouYe();
        call.enqueue(callback);
    }
    /**
     * 网络访问的方式，此处为异步    更多点击
     * @param callback
     */
    public void getVerticalShuiGuoBean(Callback<ShuiGuoBean> callback){
        Call<ShuiGuoBean> call =iDataBeanLunbo.getVerticalShuiGuo();
        call.enqueue(callback);
    }

    /**
     * 网络访问的方式，此处为异步    更多点击
     * @param callback
     */
    public void getVerticalXinXianBean(Map<String,String> params,Callback<XinXianBean> callback){
        Call<XinXianBean> call =iDataBeanLunbo.getVerticalXinXian(params);
        call.enqueue(callback);
    }
}
